package com.zhangxun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangxun.domain.ChangeDto;
import com.zhangxun.domain.Prod;
import com.zhangxun.domain.Sku;
import com.zhangxun.service.ProdService;
import com.zhangxun.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("商品管理接口")
@RestController
@RequestMapping("/prod/prod")
public class ProdProductController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProdService prodService;

    @GetMapping("page")
    @ApiOperation("查询所有商品信息，并分页")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Page<Prod> page(Page<Prod> page,Prod prod){
        page = prodService.pageByCondition(page,prod);
        return page;
    }


    @PostMapping
    @ApiOperation("保存新增的商品信息")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Boolean> save(@RequestBody Prod prod){

        boolean result = prodService.saveProd(prod);

        return ResponseEntity.ok(result);
    }

    /**
     * 远程调用通过商品id查询商品的接口
     * @param prodId
     * @return
     */
    @GetMapping("/getOne/{prodId}")
    public Prod getById(@PathVariable("prodId") Long prodId){
        return prodService.getById(prodId);
    }


    @GetMapping("/prod/prodInfo")
    @ApiOperation("通过商品id查询商品详情接口")
    public ResponseEntity<Prod> prodInfo( Long prodId){
        Prod prod = prodService.getById(prodId);
        List<Sku> skuList = skuService.list(new LambdaQueryWrapper<Sku>().eq(Sku::getProdId, prodId));
        prod.setSkuList(skuList);

        return ResponseEntity.ok(prod);
    }


    /**
     * 远程调用获取id集合对应的prod
     * @param prodIdList
     * @return
     */
    @PostMapping("/loadProdByIds")
    public List<Prod> loadProdByIds(@RequestBody List<Long> prodIdList){

        List<Prod> prodList = prodService.list(new LambdaQueryWrapper<Prod>()
                .eq(Prod::getStatus, 1)
                .in(Prod::getProdId, prodIdList));
        return prodList;
    }

    /**
     * 远程调用获取id集合对应的sku
     * @param skuIdList
     * @return
     */
    @PostMapping("/loadSkuByIds")
    public List<Sku> loadSkuByIds(@RequestBody List<Long> skuIdList){

        List<Sku> skuList = skuService.list(new LambdaQueryWrapper<Sku>()
                .eq(Sku::getStatus, 1)
                .in(Sku::getSkuId, skuIdList));

        return skuList;
    }


    /**
     * 远程调用通过id和count修改prod和sku库存
     * @param changeDto 封装商品id和库存的对象
     */
    @PostMapping("/reduceProdByIds")
    void reduceCountByIds(@RequestBody ChangeDto changeDto){
        prodService.updateCountByIds(changeDto);
    }

}
